package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.IdiomStoryEntity;
import com.fenghuajueli.idiomppp.utils.DateUtils;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;

import java.util.List;

/**
 * 经典故事数据适配器
 */
public class ClassicStoryListAdapter extends BaseAdapter {

    private List<IdiomStoryEntity> idiomStoryEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private OnListClickListener<IdiomStoryEntity> onListClickListener;
    private Boolean isShowAll = false;

    public ClassicStoryListAdapter(Context context, List<IdiomStoryEntity> idiomStoryEntityList) {
        this.idiomStoryEntityList = idiomStoryEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void refreshData(List<IdiomStoryEntity> idiomStoryEntityList) {
        this.idiomStoryEntityList = idiomStoryEntityList;
        notifyDataSetChanged();
    }

    public void setShowAll(Boolean showAll) {
        isShowAll = showAll;
    }

    @Override
    public int getCount() {
        return idiomStoryEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return idiomStoryEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void setOnListClickListener(OnListClickListener<IdiomStoryEntity> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.classic_story_item_list_layout, null);
            holder.tvIdiomName = convertView.findViewById(R.id.tvIdiomName);
            holder.storyImg = convertView.findViewById(R.id.storyImg);
            holder.tvStoryDesc = convertView.findViewById(R.id.tvStoryDesc);
            holder.tvCategory = convertView.findViewById(R.id.tvCategory);
            holder.itemContainer = convertView.findViewById(R.id.itemContainer);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IdiomStoryEntity idiomStoryEntity = idiomStoryEntityList.get(position);
        holder.tvIdiomName.setText(idiomStoryEntity.getTitle());
        holder.tvStoryDesc.setText(idiomStoryEntity.getContent());
        ImageLoadManager.loadForImageView(holder.storyImg, getImg(position));
        holder.itemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onListClickListener != null) {
                    onListClickListener.itemClick(position, idiomStoryEntity);
                }
            }
        });
        int showCount = position * 20 + DateUtils.getDayInYear() + 500;
        holder.tvCategory.setText(showCount + "人浏览 | " + idiomStoryEntity.getTitle_type());
        return convertView;
    }

    final class ViewHolder {
        TextView tvIdiomName;
        ImageView storyImg;
        TextView tvStoryDesc;
        TextView tvCategory;
        RelativeLayout itemContainer;
    }


    private int getImg(int pos){
        int img = R.mipmap.aa_fm_gstx;
        switch (pos % 8){
            case 0 :
                img = R.mipmap.aa_fm_snsq;
                break;
            case 1 :
                img = R.mipmap.aa_fm_gstx;
                break;
            case 2 :
                img = R.mipmap.aa_fm_hldj;
                break;
            case 3 :
                img = R.mipmap.aa_fm_ktpd;
                break;
            case 4 :
                img = R.mipmap.aa_fm_njgc;
                break;
            case 5 :
                img = R.mipmap.aa_fm_qnbz;
                break;
            case 6 :
                img = R.mipmap.aa_fm_yedl;
                break;
            case 7 :
                img = R.mipmap.aa_fm_zstb;
                break;

        }
        return img;
    }

}
